package com.mall.commons.common;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName CommonPage
 * @Description 分页对象
 * @Author Young
 * @Date 2023/3/11 17:19
 * @Version 1.0
 */
@Data
public class CommonPage<T> {
    private List<T> list;
    private Long pageNum;//第几页
    private Long pageSize;//每页条数
    private Long total;//总条数
    private Long totalPag;//总页数

    public CommonPage() {
    }

    public CommonPage(Long pageNum, Long pageSize, Long total) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
    }

    public CommonPage(List<T> list, Long pageNum, Long pageSize, Long total, Long totalPag) {
        this.list = list;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.totalPag = totalPag;
    }

    /**
     * 获取分页对象
     * @param list 集合
     * @param pageNum 第几页
     * @param pageSize 没有条数
     * @param total 总条数
     * @param totalPag 总页数
     * @return CommonPage
     */
    public static<T> CommonPage init(List<T> list, Long pageNum, Long pageSize, Long total, Long totalPag) {
        CommonPage commonPage = new CommonPage();
        commonPage.setList(list);
        commonPage.setPageNum(pageNum);
        commonPage.setPageSize(pageSize);
        commonPage.setTotal(total);
        commonPage.setTotalPag(totalPag);
        return commonPage;
    }

    /**
     * 原始Page传入转换MyPage
     * @param page 原生Page
     * @return CommonPage
     */
    public static<T> CommonPage getPage(Page<T> page) {
        CommonPage commonPage = new CommonPage(page.getCurrent(),
                page.getSize(),
                page.getTotal());
        commonPage.setList(page.getRecords());
        long totalPag = page.getTotal() % page.getSize() == 0 ? page.getTotal() / page.getSize() : page.getTotal() / page.getSize() + 1;
        commonPage.setTotalPag(totalPag);

        return commonPage;
    }
}
